package com.smartEdu.controller.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.commons.Tag_Tips_web;
import com.smartEdu.commons.constant.JspView;
import com.smartEdu.mapper.common.VideoInfoMapper;
import com.smartEdu.po.VideoInfoPo;
import com.smartEdu.service.LoginService;
import com.smartEdu.vo.web.LoginVo;

@Controller
//@RequestMapping(value="/web/")
public class LoginController {
	
	@Autowired
	@Qualifier(value="loginServiceWeb")
	public LoginService loginService;
	
	@Autowired
	private VideoInfoMapper mapper;
	
	/**
	 * 登录页面展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="login")
	public String login(HttpServletRequest request,HttpServletResponse response) {
		return JspView.LOGIN.getName();
	}
	
	/**
	 * 登录认证中转控制器
	 * @param loginVo
	 * @param request
	 * @param attr
	 * @return
	 */
	@RequestMapping(value="loginauth")
	public String loginauth(@ModelAttribute("login") LoginVo loginVo, HttpServletRequest request, RedirectAttributes attr) {
		if (loginService.loginauth(loginVo)) {
			HttpSession httpsess = request.getSession();
			httpsess.setAttribute(Tag_Tips.HASLOGIN, System.currentTimeMillis());
			httpsess.setAttribute(Tag_Tips_web.LOGINUSER, loginVo.getUsername());
			httpsess.setAttribute(Tag_Tips_web.LOGINAME, loginVo.getLoginname());
//			attr.addFlashAttribute("uid", loginVo.getUsername());
//			for (VideoInfoPo it : mapper.findByCreator("02018")) {
//				System.err.println(it.getPk());
//			}
			
			return "redirect:/web/dashboard";
		}
		attr.addFlashAttribute("respcode", "1");
		attr.addFlashAttribute("respmsg", "账号与密码不相符");
		return "redirect:/login";
	}
	
	@RequestMapping(value="test")
	public String test() {
		return "/manager/elements";
	}
}
